package com.zj.framework.okhttp;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.message.BasicNameValuePair;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;

/**
 * @author xi.yang(xi.yang@downjoy.com)
 * @create 2018-05-29 下午 3:24
 **/
public class HttpClientUtil {
    public static void main(String[] args) {
        Map<String,String> cookies = getCookies();
        Map<String, String> nvps = new HashMap<>();
        nvps.put("tag","29");
        nvps.put("gcid","7");
        nvps.put("siteid","44690220");
        nvps.put("selectact","add");
        nvps.put("t",System.currentTimeMillis()+"");
        nvps.put("_tb_token_","e7de57ee33b33");
        nvps.put("pvid","22_118.144.66.196_474_1527576780122");
        for (int i = 143; i < 1000; i++) {
            nvps.put("newadzonename","导购"+i);
            nvps.put("t",System.currentTimeMillis()+"");
            boolean result = postPid(cookies, nvps);
            if (!result) {
                break;
            }
            try {
                Thread.sleep((new Random().nextInt(5)+5)*1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public static Map<String,String> getCookies() {
        Map<String,String> cookies = new HashMap<>();
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\149_Full.txt")),
                    "UTF-8"));
            String lineTxt = null;
            while ((lineTxt = br.readLine()) != null) {
                if(lineTxt.contains("Cookie")){
                    lineTxt = lineTxt.replace("Cookie:","");
                    String[] strs = lineTxt.split(";");
                    for (String str : strs) {
                        String[] kv = str.split("=");
                        cookies.put(kv[0].trim(),kv[1].trim());
                    }
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cookies;
    }

    public static boolean postPid(Map<String,String> cookiemap,Map<String, String> nvps) {
        CookieStore cookieStore = new BasicCookieStore();
        CloseableHttpClient httpClient = HttpClients.custom()
                .setDefaultCookieStore(cookieStore)
                .build();
        try {

            HttpPost post = new HttpPost("http://pub.alimama.com/common/adzone/selfAdzoneCreate.json");
            for (String k : cookiemap.keySet()) {
                BasicClientCookie cookie = new BasicClientCookie(k,cookiemap.get(k));
                cookie.setVersion(0);
                cookie.setDomain(".alimama.com");   //设置范围
                cookie.setPath("/");
                cookieStore.addCookie(cookie);
            }
            List<NameValuePair> nvpsList = new ArrayList<NameValuePair>();
            if(nvps != null){
                for (String key : nvps.keySet()) {
                    nvpsList.add(new BasicNameValuePair(key, nvps.get(key)));
                }
            }
            post.setEntity(new UrlEncodedFormEntity(nvpsList, "UTF-8"));
            HttpResponse response = httpClient.execute(post);//
            System.out.println(response.toString());
            if (response.getStatusLine().getStatusCode() == 200) {
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{

        }
        return false;
    }
}
